use "$data\asi2005_productlevel04082013.dta", clear
drop District

append using "$data\sum2005_productlevel04082013.dta"
replace state=State if missing(state)


	gen product_nec = strmatch( desc,"*n.e.c*")
	gen product_other = strmatch( desc,"*other*")
	gen product_nec_other = max( product_nec,product_other)
	tab survey product_nec_other
	drop if product_nec_other==1
	
	drop if ln_price==.|ln_L==.

	gen counter = 1
	sort survey product_unit_cat ln_price
	by survey product_unit_cat: gen product_counter = sum(counter)
	egen product_no_obs = max(product_counter), by (survey product_unit_cat)
	
		* Yogurt Example
	gen ln_price_temp  = ln_price+log(1000) if asicc==11401
// 	scatter ln_price_temp ln_L if survey=="ASI"&asicc==11401, graphregion(color(white)) title("") name(milk, replace) xtitle("Log of Employment", size(4.3)) ytitle("Log of Price", size(4.3))
// 	graph export "$graphs\milk.eps", name(milk) as(eps) replace
	reg ln_price_temp ln_L if survey=="ASI"&asicc==11401
	reg ln_price_temp ln_L if survey=="ASI"&asicc==11401&ln_price_temp>8
	reg ln_price_temp ln_L if survey=="ASI"&asicc==11401&ln_price_temp<8
	replace ln_price_temp = ln_price_temp + log(1000) if survey=="ASI"&asicc==11401&ln_price_temp<8
	reg ln_price_temp ln_L if survey=="ASI"&asicc==11401

		* Bring in coding I had done to mark products for which units were messed up in the ASI
	merge m:1 product_unit_cat using "$data\product units problem\summary.dta"
	* Products with very few ASI observation not matched. There seemed to be 2 ASICC's with 10 observations which were missing in the summary file.
	tab survey if _merge==1
	tab product_no_obs if _merge~=3&survey=="ASI"
	drop if _merge==2
	
	
	drop if product_no_obs<10&survey=="ASI"
	drop if _merge~=3&survey=="ASI" 
	
	gen product_unit_cat_old = product_unit_cat
	replace product_unit_cat = product_unit_cat*10 + 1 if ln_price>cutoffprice&(problem==1|problem==2|problem==3)
	replace product_unit_cat = product_unit_cat*10 + 2 if ln_price<=cutoffprice&(problem==1|problem==2|problem==3)
			
			
	drop counter product_counter product_no_obs
	gen ln_L_winsor = ln_L
	su ln_L if survey=="ASI", de
	replace ln_L_winsor = `r(p1)' if ln_L<=`r(p1)'&survey=="ASI"
	replace ln_L_winsor = `r(p99)' if ln_L>=`r(p99)'&survey=="ASI"
	su ln_L_winsor if survey=="ASI", de
	su ln_L if survey=="SUM", de
	replace ln_L_winsor = `r(p1)' if ln_L<=`r(p1)'&survey=="SUM"
	replace ln_L_winsor = `r(p99)' if ln_L>=`r(p99)'&survey=="SUM"
	su ln_L_winsor if survey=="SUM", de


		* Winsorize prices within survey,product,unit categories
		gen counter = 1
		sort survey product_unit_cat ln_price
		by survey product_unit_cat: gen product_counter = sum(counter)
		egen product_no_obs = max(product_counter), by (survey product_unit_cat)
		foreach per_drop in 1 5 {
			gen obs_winsor_low_bound`per_drop' = floor(product_no_obs*`per_drop'/100) + 1 
			gen obs_winsor_upper_bound`per_drop' =  product_no_obs -  obs_winsor_low_bound`per_drop'
			gen ln_price_lowbound_temp`per_drop' = ln_price if obs_winsor_low_bound`per_drop'==product_counter
			egen ln_price_lowbound`per_drop' = min(ln_price_lowbound_temp`per_drop') if obs_winsor_low_bound`per_drop'>=product_counter,by(survey product_unit_cat)
			gen ln_price_upbound_temp`per_drop' = ln_price if obs_winsor_upper_bound`per_drop'==product_counter
			egen ln_price_upbound`per_drop' = min(ln_price_upbound_temp`per_drop') if obs_winsor_upper_bound`per_drop'<=product_counter,by(survey product_unit_cat)
			gen ln_price_winsor`per_drop' = ln_price
			replace ln_price_winsor`per_drop' = ln_price_lowbound`per_drop' if ln_price_lowbound`per_drop'~=.
			replace ln_price_winsor`per_drop' = ln_price_upbound`per_drop' if ln_price_upbound`per_drop'~=.
		}
		gen rural=Sector
				xi: areg ln_price_winsor1 ln_L_winsor i.state*i.rural if survey=="ASI", absorb(product_unit_cat_old) vce(cluster product_unit_cat_old)
		outreg2 using "$results\product_12split`split_prob_1_2'_4split`csplit_prob_4'", replace keep(ln_L_winsor) addst(Cluster, e(N_clust), FE, e(df_a)) excel stats(coef se pval) bdec(3) sdec(4) ctitle("ASI, No Corr, Winsor 1")
		*areg ln_price_winsor1 ln_L_winsor i.state*i.rural if survey=="SUM", absorb(product_unit_cat_old) vce(cluster product_unit_cat_old)
		*outreg2 using "$results\product_12split`split_prob_1_2'_4split`csplit_prob_4'", append keep(ln_L_winsor) addst(Cluster, e(N_clust), FE, e(df_a)) excel stats(coef se pval) bdec(3) sdec(4) ctitle("NSS, No Corr, Winsor 1")
		xi: areg ln_price_winsor1 ln_L_winsor i.state*i.rural, absorb(product_unit_cat_old) vce(cluster product_unit_cat_old)
		outreg2 using "$results\product_12split`split_prob_1_2'_4split`csplit_prob_4'", append keep(ln_L_winsor) addst(Cluster, e(N_clust), FE, e(df_a)) excel stats(coef se pval) bdec(3) sdec(4) ctitle("Combined, No Corr, Winsor 1")
**# Bookmark #1 The next three regressions are in the paper

label variable ln_fixedPlantGrossOpen "Machinery Capital"
label variable ln_fixedPlantGrossAdd "Machinery Investment"
label variable ln_fixedTotalGrossOpen "Total Capital"
label variable ln_fixedTotalGrossAdd "Total Capital Investment"


gen total_capital=fixedTotalGrossOpen
// replace total_capital=fixedTotalOwnValue if survey=="SUM"
gen ln_total_capital=ln(total_capital)

gen total_machinery=ln_fixedPlantGrossOpen
// replace total_machinery=fixedPlantOwnValue if survey=="SUM"
gen ln_total_machinery=ln(total_machinery)
	
eststo clear
local variables_interest fixedPlantGrossOpen fixedPlantGrossAdd fixedTotalGrossOpen fixedTotalGrossAdd
foreach var of varlist `variables_interest'{
	eststo: xi: reghdfe ln_price_winsor1 ln_`var' i.state*i.rural, absorb(product_unit_cat) vce(cluster product_unit_cat_old)
	estadd local Fixed1 "Yes"
	estadd local Fixed2 "Yes"
	estadd local Cluster "Product"
}
esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Fixed1 State x Rural FE" "Fixed2 Product FE" "Cluster SE clusters:") drop(_*)


**# Bookmark #3 Firms with labor use more capital per unit of output

gen K_Q= fixedTotalGrossOpen / productQuantity_con
winsor2 K_Q, cuts(5 95)
gen lnK_Q=ln(K_Q)

gen I_Q= fixedTotalGrossAdd / productQuantity_con
winsor2 I_Q, cuts(5 95)
gen lnI_Q=ln(I_Q)
egen fixed_effect=group(state rural product_unit_cat)
egen fixed_effect_state_rural=group(state rural)  
xi: reg K_Q_w ln_L_winsor i.state*i.rural if strpos(desc, "cloth finished") > 0 &!missing(ln_fixedTotalGrossOpen)
xi: reg K_Q_w ln_L_winsor i.state*i.rural if !missing(ln_fixedTotalGrossOpen), absorb(product_unit_cat_old) vce(cluster product_unit_cat_old)
binscatter K_Q_w ln_L_winsor, absorb(fixed_effect) xtitle("log(Labor), Residualized")  ytitle("Capital per unit of Output, Residualized") noa
graph export $results\Capital_per_output_labor.png, replace
binscatter K_Q ln_L_winsor, absorb(fixed_effect) xtitle("log(Labor), Residualized")  ytitle("Capital per unit of Output, Residualized") noa
graph export $results\Capital_per_output_labor_nonwinsor.png, replace
binscatter lnK_Q ln_L_winsor, absorb(fixed_effect) xtitle("log(Labor), Residualized")  ytitle("log(Capital per unit of Output), Residualized") noa
graph export $results\F6_Capital_per_output_labor_ln.png, replace

binscatter lnK_Q ln_L_winsor if product_unit_cat==6330503&survey=="ASI", xtitle("log(Labor), Residualized")  ytitle("log(Capital per unit of Output), Residualized")
graph export $results\F16_Capital_per_output_labor_ln_cloth.png, replace

binscatter K_Q_w ln_L_winsor if strpos(desc, "cloth finished") > 0 &!missing(ln_fixedTotalGrossOpen), absorb(fixed_effect_state_rural)


label variable ln_price_winsor1 "log(price)"
label variable ln_L_winsor "log(labor)"
label variable K_Q "Capital per unit of output"
label variable K_Q_w "Capital per unit of output"
label variable I_Q_w "Investment per unit of output"
label variable lnK_Q "log (Capital per unit of output)"
label variable lnI_Q "log (Investment per unit of output)"

eststo clear
eststo: reghdfe K_Q_w ln_L_winsor if !missing(ln_fixedTotalGrossOpen), absorb(product_unit_cat#state#rural) vce(cluster product_unit_cat_old)
estadd scalar Observations=e(N_full)
estadd scalar Single=e(num_singletons) 
	estadd local Fixed3 "Yes"
	estadd local Products "1201"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Product"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
eststo: reghdfe lnK_Q ln_L_winsor if !missing(ln_fixedTotalGrossOpen), absorb(product_unit_cat#state#rural) vce(cluster product_unit_cat_old)
esttab 
estadd scalar Observations=e(N_full)
	estadd scalar Single=e(num_singletons) 
	estadd local Fixed3 "Yes"
	estadd local Products "1201"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "No"
	estadd local Cluster "Product"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
eststo: reghdfe I_Q_w ln_L_winsor if !missing(ln_fixedTotalGrossOpen), absorb(product_unit_cat#state#rural) vce(cluster product_unit_cat_old)
estadd scalar Observations=e(N_full)
estadd scalar Single=e(num_singletons) 
	estadd local Fixed3 "Yes"
	estadd local Products "1201"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Product"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
eststo: reghdfe lnI_Q ln_L_winsor if !missing(ln_fixedTotalGrossOpen), absorb(product_unit_cat#state#rural) vce(cluster product_unit_cat_old)
estadd scalar Observations=e(N_full)
estadd scalar Single=e(num_singletons) 
	estadd local Fixed3 "Yes"
	estadd local Products "1201"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "No"
	estadd local Cluster "Product"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Fixed1 State x Rural FE" "Fixed2 Product FE" "Cluster SE clusters:") drop(_*)

// 	esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) scalars("FE Fixed Effects:") nocon label legend replace order(ln_pce365 ) 
	esttab, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Winsor Winsor" Observations "k \hline " "Fixed3 State x Rural x Product FE" "Products Number of Products" "Cluster SE clusters:"   "NumClust Number of Clusters" "Sample Sample")
		esttab using $results\T7_20250313_K_Q_L.tex, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Winsor Winsor" Observations "k \hline " "Fixed3 State x Rural x Product FE" "Cluster SE clusters:"   "NumClust Number of Clusters" "Sample Sample")
		
		

bys state rural product_unit_cat: egen total_prod_state_rural=total(productQuantity_con)
gen share_prod_state_rural=productQuantity_con/total_prod_state_rural
gen share_prod_state_rural_squared=share_prod_state_rural^2
bys state rural product_unit_cat: egen HHI_state_rural_prod=total(share_prod_state_rural_squared)
gen lnL_HHI=HHI_state_rural_prod*ln_L_winsor

xi: reghdfe ln_price_winsor1 ln_L_winsor HHI_state_rural_prod lnL_HHI i.state*i.rural if survey=="ASI", absorb(product_unit_cat_old) vce(cluster product_unit_cat_old)


gen ln_productQuantity_con= ln(productQuantity_con)



**# Making output price graphs for cloth
preserve
keep if product_unit_cat==6330503&survey=="ASI"
eststo clear
eststo: qui xi: reg ln_price_winsor1 ln_fixedTotalGrossOpen  i.state*i.rural  if !missing(ln_fixedTotalGrossOpen)
eststo: qui xi: reg ln_price_winsor1 ln_fixedTotalGrossAdd  i.state*i.rural  if !missing(ln_fixedTotalGrossOpen)
eststo: qui xi: reg ln_price_winsor1 ln_L_winsor  i.state*i.rural  if !missing(ln_fixedTotalGrossOpen)
esttab, drop(_*)


binscatter   ln_price_winsor1  ln_L_winsor, absorb(fixed_effect_state_rural) noa xtitle(log (labor employed) Residualized) ytitle(log(price) Residualized)
graph export $results\F15_output_prices_labor_asicc_63305.png, replace
// hist ln_price_winsor1, xtitle(log(price) for finished cloth)
// graph export $results\ln_cloth_prices_hist.png, replace
hist price, xtitle(Price for finished cloth)
graph export $results\F14a_cloth_prices_hist.png, replace
restore



**Firms with more capital per labor sell more expensive products too
// binscatter  ln_price_winsor1 ln_fixedTotalGrossOpen_L_ratio if !missing(ln_fixedTotalGrossOpen)&survey=="ASI", xtitle(log (Total capital/Labor)) ytitle(log (Product price))

sum price if product_unit_cat==6330503&survey=="ASI" , detail
sum L if product_unit_cat==6330503&survey=="ASI" , detail
codebook L if product_unit_cat==6330503&survey=="ASI" , detail

hist L if product_unit_cat==6330503&survey=="ASI",  width(300) xtitle(Labor)
graph export $results\F14b_Labor_employed_cloth.png, replace   


hist ln_L_winsor if product_unit_cat==6330503&survey=="ASI", xtitle(log(Labor))
graph export $results\ln_Labor_employed_cloth.png, replace  

eststo clear
local variables_interest fixedPlantGrossOpen fixedPlantGrossAdd fixedTotalGrossOpen fixedTotalGrossAdd
foreach var of varlist `variables_interest'{
	cap gen `var'_L_ratio=`var'/L
	eststo: xi: reghdfe ln_price_winsor1 `var'_L_ratio  if survey=="ASI", absorb(state#rural#product_unit_cat) vce(cluster product_unit_cat_old)
	estadd local Fixed1 "Yes"
	estadd local Fixed2 "Yes"
	estadd local Cluster "Product"
}
label variable fixedPlantGrossOpen_L_ratio "Machinery Capital/ Labor ratio"
label variable fixedPlantGrossAdd_L_ratio "Machinery Investment/ Labor ratio"
label variable fixedTotalGrossOpen_L_ratio "Total Capital/ Labor ratio"
label variable fixedTotalGrossAdd_L_ratio "Total Capital Investment/ Labor ratio"
esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Fixed1 State x Rural FE" "Fixed2 Product FE" "Cluster SE clusters:") drop(_*)


eststo clear
local variables_interest fixedPlantGrossOpen fixedPlantGrossAdd fixedTotalGrossOpen fixedTotalGrossAdd
foreach var of varlist `variables_interest'{
	cap gen `var'_L_ratio=`var'/L
	eststo: reghdfe ln_price_winsor1 `var'_L_ratio  if survey=="ASI"&product_unit_cat==6330503, absorb(state#rural) vce(robust)
	estadd local Fixed1 "Yes"
	estadd local Fixed2 "Yes"
	estadd local Cluster "Product"
}
label variable fixedPlantGrossOpen_L_ratio "Machinery Capital/ Labor ratio"
label variable fixedPlantGrossAdd_L_ratio "Machinery Investment/ Labor ratio"
label variable fixedTotalGrossOpen_L_ratio "Total Capital/ Labor ratio"
label variable fixedTotalGrossAdd_L_ratio "Total Capital Investment/ Labor ratio"
esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Fixed1 State x Rural FE" "Fixed2 Product FE" "Cluster SE clusters:") drop(_*)


label variable ln_price_winsor1 "log(price)"
eststo clear
local variables_interest fixedPlantGrossOpen fixedPlantGrossAdd fixedTotalGrossOpen fixedTotalGrossAdd
foreach var of varlist `variables_interest'{
	replace `var'_L_ratio=`var'/L/1000000
	cap gen ln_`var'_L_ratio=ln(`var'/L/1000000)
	eststo: xi: reghdfe ln_price_winsor1 `var'_L_ratio  if survey=="ASI", absorb(product_unit_cat#state#rural) vce(cluster product_unit_cat)
estadd scalar Observations=e(N_full)
	estadd scalar Single=e(num_singletons) 
	estadd local Fixed3 "Yes"
	estadd local Products "1218"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Product"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
}
label variable fixedPlantGrossOpen_L_ratio "Machinery Capital/ Labor ratio"
label variable fixedPlantGrossAdd_L_ratio "Machinery Investment/ Labor ratio"
label variable fixedTotalGrossOpen_L_ratio "Total Capital/ Labor ratio"
label variable fixedTotalGrossAdd_L_ratio "Total Capital Investment/ Labor ratio"
esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Fixed1 State x Rural FE" "Fixed2 Product FE" "Cluster SE clusters:") drop(_*)

// 	esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) scalars("FE Fixed Effects:") nocon label legend replace order(ln_pce365 ) 
	esttab, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Winsor Winsor" Observations "k \hline " "Fixed3 State x Rural x Product FE" "Products Number of Products" "Cluster SE clusters:"   "NumClust Number of Clusters" "Sample Sample")
		esttab using $results\T8_20250306_output_prices_capital.tex, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Winsor Winsor" Observations "k \hline " "Fixed3 State x Rural x Product FE" "Cluster SE clusters:"   "NumClust Number of Clusters" "Sample Sample")
		

**# Cloth regressions Appendix
		
preserve
keep if product_unit_cat==6330503&survey=="ASI"
	label variable ln_price_winsor1 "log(price)"
eststo clear
local variables_interest fixedPlantGrossOpen fixedPlantGrossAdd fixedTotalGrossOpen fixedTotalGrossAdd
foreach var of varlist `variables_interest'{
// 	cap gen `var'_L_ratio=`var'/L/1000000
// 	cap gen ln_`var'_L_ratio=ln(`var'/L/1000000)
	eststo: xi: reghdfe ln_price_winsor1 `var'_L_ratio  if survey=="ASI", absorb(state#rural) vce(robust)
estadd scalar Observations=e(N_full)
	estadd scalar Single=e(num_singletons) 
	estadd local Fixed3 "Yes"
	estadd local Products "1218"
	estadd local PR50_5=round(exp(e(b)[1,1]*6.96)/exp(e(b)[1,1]*6.17),0.01)
	estadd local PR500_5=round(exp(e(b)[1,1]*7.686)/exp(e(b)[1,1]*5.75),0.01)
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Robust"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
}
label variable fixedPlantGrossOpen_L_ratio "Machinery Capital/ Labor ratio"
label variable fixedPlantGrossAdd_L_ratio "Machinery Investment/ Labor ratio"
label variable fixedTotalGrossOpen_L_ratio "Total Capital/ Labor ratio"
label variable fixedTotalGrossAdd_L_ratio "Total Capital Investment/ Labor ratio"
esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Fixed1 State x Rural FE" "Fixed2 Product FE" "Cluster SE clusters:") drop(_*)

// 	esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) scalars("FE Fixed Effects:") nocon label legend replace order(ln_pce365 ) 
	esttab, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Winsor Winsor" Observations "k \hline " "Fixed3 State x Rural x Product FE" "Products Number of Products" "Cluster SE clusters:"   "NumClust Number of Clusters" "Sample Sample")
		esttab using $results\20250306_output_prices_capital_cloth.tex, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Winsor Winsor" Observations "k \hline " "Fixed3 State x Rural FE" "Cluster SE:"  "Sample Sample")
		
		


eststo clear
eststo: reghdfe K_Q_w ln_L_winsor if !missing(ln_fixedTotalGrossOpen), absorb(state#rural) vce(robust )
estadd scalar Observations=e(N_full)
estadd scalar Single=e(num_singletons) 
	estadd local Fixed3 "Yes"
	estadd local Products "1201"
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Robust"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
eststo: reghdfe lnK_Q ln_L_winsor if !missing(ln_fixedTotalGrossOpen), absorb(state#rural) vce(robust)
esttab 
estadd scalar Observations=e(N_full)
	estadd scalar Single=e(num_singletons) 
	estadd local Fixed3 "Yes"
	estadd local Products "1201"
	estadd local Fixed4 "Yes"
	estadd local Winsor "No"
	estadd local Cluster "Robust"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
eststo: reghdfe I_Q_w ln_L_winsor if !missing(ln_fixedTotalGrossOpen), absorb(state#rural) vce(robust)
estadd scalar Observations=e(N_full)
estadd scalar Single=e(num_singletons) 
	estadd local Fixed3 "Yes"
	estadd local Products "1201"
	estadd local Fixed4 "Yes"
	estadd local Winsor "Yes"
	estadd local Cluster "Robust"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
eststo: reghdfe lnI_Q ln_L_winsor if !missing(ln_fixedTotalGrossOpen), absorb(state#rural) vce(robust)
estadd scalar Observations=e(N_full)
estadd scalar Single=e(num_singletons) 
	estadd local Fixed3 "Yes"
	estadd local Products "1201"
	estadd local Fixed4 "Yes"
	estadd local Winsor "No"
	estadd local Cluster "Robust"
	estadd local Sample "ASI"
	estadd local NumClust=e(N_clust)
esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Fixed1 State x Rural FE" "Fixed2 Product FE" "Cluster SE clusters:") drop(_*)

// 	esttab, se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) scalars("FE Fixed Effects:") nocon label legend replace order(ln_pce365 ) 
	esttab, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Winsor Winsor" Observations "k \hline " "Fixed3 State x Rural x Product FE" "Products Number of Products" "Cluster SE clusters:"   "NumClust Number of Clusters" "Sample Sample")
		esttab using $results\20250313_K_Q_L_cloth.tex, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("Winsor Winsor" Observations "k \hline " "Fixed3 State x Rural FE" "Cluster SE:" "Sample Sample")		
restore
		
		
**# Bookmark #1 **Firms with more capital per labor sell more expensive products too

binscatter  ln_price_winsor1 ln_fixedTotalGrossOpen_L_ratio if !missing(fixedTotalGrossAdd_L_ratio)&survey=="ASI", absorb(fixed_effect) xtitle(log (Total capital/Labor) Residualized) ytitle(log (Product price) Residualized) noa
graph export $results\F7_output_prices_capital_ratio.png, replace


binscatter  ln_price_winsor1 ln_fixedTotalGrossOpen_L_ratio if !missing(ln_fixedTotalGrossOpen)&survey=="ASI", xtitle(log (Total capital/Labor)) ytitle(log (Product price)) absorb(state)

// xi: areg ln_price_winsor1 ln_fixed i.state*i.rural  if survey=="ASI", absorb(product_unit_cat) vce(cluster product_unit_cat_old)

// **Reg for chappals 1530802
// reghdfe ln_price_winsor1 ln_L_winsor if product_unit_cat_old==1530602, absorb(state#rural)
// **Reg for finished cloth
// reghdfe ln_price_winsor1 ln_L_winsor if product_unit_cat==6330503&survey=="ASI", absorb(state#rural)


****Making figures for product regressions
tab product_unit_cat, gen(II)
bys product_unit_cat: gen obs_product=_N

gen product_unit_cat_coeff=.
forv i=1(1)3182{
	local iter `=`iter'+1'
	capture noisily reghdfe ln_price_winsor1 ln_L_winsor  if survey=="ASI"& II`i'==1, absorb(state#rural)
	capture noisily replace product_unit_cat_coeff=e(b)[1,1] if _n==`iter'
}

sum product_unit_cat_coeff if survey=="ASI", det
tab product_unit_cat_coeff if survey=="ASI"

** Histogram of the bank-specific coefficients
label variable product_unit_cat_coeff "Item-code-specific elasticity"
hist product_unit_cat_coeff if survey=="ASI" & product_unit_cat_coeff>-2&product_unit_cat_coeff<1.85, freq xtitle("Product-specific price elasticity to firm size") xlabel(,nogrid) ylabel(,nogrid) bins(10)
graph export $results\F8_20250306_Histogram_producer_elasticity.png, replace

// hist product_unit_cat_coeff if survey=="ASI" &obs_product>400& product_unit_cat_coeff>-2&product_unit_cat_coeff<1.85, freq xtitle("Product-specific elasticity to firm size") xlabel(,nogrid) ylabel(,nogrid) bins(10)


label variable ln_price_winsor1 "log(output price)"
label variable ln_L_winsor "log(labor)"
egen unit_fe=group(unit_asi_05)
*****Looking at less precise measures of "product"
eststo clear
eststo: reghdfe ln_price_winsor1 ln_L_winsor  if survey=="ASI", absorb(product_unit_cat#state#rural) vce(cluster product_unit_cat_old)
	estadd scalar Observations=e(N_full)
	estadd local Fixed1 "Yes"
	estadd local Fixed2 "N/A"
	estadd local Fixed3 "N/A"
	estadd local Fixed4 "N/A"
	estadd local Cluster "Product"
	estadd local Sample "ASI"
eststo: reghdfe ln_price_winsor1 ln_L_winsor if survey=="ASI", absorb(state#rural#nic04_4d#unit_fe) vce(cluster product_unit_cat_old)
	estadd scalar Observations=e(N_full)
	estadd local Fixed1 "No"
	estadd local Fixed2 "Yes"
	estadd local Fixed3 "N/A"
	estadd local Fixed4 "Yes"
	estadd local Cluster "Product"
	estadd local Sample "ASI"
eststo: reghdfe ln_price_winsor1 ln_L_winsor if survey=="ASI", absorb(state#rural#nic04_2d#unit_fe) vce(cluster product_unit_cat_old)
	estadd scalar Observations=e(N_full)
	estadd local Fixed1 "No"
	estadd local Fixed2 "No"
	estadd local Fixed3 "Yes"
	estadd local Fixed4 "Yes"
	estadd local Cluster "Product"
	estadd local Sample "ASI"
esttab, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars(Observations "Sample Sample" "Fixed1 State x Rural FE  x Product FE" "Fixed2 State x Rural x Product FE" "Fixed2 State x Rural x 4-digit NIC x Unit FE" "Fixed3 State x Rural FE x 2-digit NIC code x Unit FE" "Cluster SE clusters:")  
esttab using $results\T10_20250306_broader_product_tables.tex, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars(Observations "Sample Sample" "Fixed1 State x Rural x Product FE" "Fixed2 State x Rural x 4-digit NIC x Unit FE" "Fixed3 State x Rural x 2-digit NIC x Unit FE" "Cluster SE clusters:") 
esttab using $results\20250306_broader_product_tables.rtf, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars(Observations "Sample Sample" "Fixed1 State x Rural x Product FE" "Fixed2 State x Rural x 4-digit NIC x Unit FE" "Fixed3 State x Rural x 2-digit NIC x Unit FE" "Cluster SE clusters:") 

		codebook product_unit_cat  if survey=="ASI"
		codebook product_unit_cat  if survey=="SUM"
		codebook product_unit_cat  
		
		eststo clear
		eststo: reghdfe ln_price_winsor1 ln_L_winsor if survey=="ASI", absorb(product_unit_cat#state#rural) vce(cluster product_unit_cat_old)
		estadd scalar Observations=e(N_full)
		estadd scalar Single=e(num_singletons) 
		estadd local Fixed1 "Yes"
		estadd local PR50_5=round(exp(e(b)[1,1]*ln(50))/exp(e(b)[1,1]*ln(5)),0.01)
		estadd scalar PR500_5=round(exp(e(b)[1,1]*ln(500))/exp(e(b)[1,1]*ln(5)),0.01)
		estadd local Fixed4 "Yes"
		estadd local Winsor "Yes"
		estadd local Cluster "Product"
		estadd local Sample "ASI"
		estadd local NumClust=e(N_clust)
		eststo: reghdfe ln_price_winsor1 ln_L_winsor if survey=="SUM", absorb(product_unit_cat#state#rural) vce(cluster product_unit_cat_old)
		estadd scalar Observations=e(N_full)	
		estadd local Fixed1 "Yes"
		estadd local Sample "SUM"
		estadd local NumClust=e(N_clust)
		estadd local Cluster "Product"
		estadd local Winsor "Yes"
		estadd local PR50_5=round(exp(e(b)[1,1]*ln(50))/exp(e(b)[1,1]*ln(5)),0.01)
		estadd local PR500_5=round(exp(e(b)[1,1]*ln(500))/exp(e(b)[1,1]*ln(5)),0.01)
eststo: reghdfe ln_price_winsor1 ln_L_winsor, absorb(product_unit_cat#state#rural) vce(cluster product_unit_cat_old)
		estadd scalar Observations=e(N_full)	
		estadd local Fixed1 "Yes"
		estadd local Sample "Both"
		estadd local NumClust=e(N_clust)
		estadd local Cluster "Product"
		estadd local Winsor "Yes"
		estadd local PR50_5=round(exp(e(b)[1,1]*ln(50))/exp(e(b)[1,1]*ln(5)),0.01)
		estadd local PR500_5=round(exp(e(b)[1,1]*ln(500))/exp(e(b)[1,1]*ln(5)),0.01)

		esttab, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("PR50_5 Price Ratio (Size 50 to 5)" "PR500_5 Price Ratio (Size 500 to 5)" "j \hline" "Sample Sample"  "Winsor Winsor" Observations "k \hline "   "Fixed1 State x Rural x Product FE" "Products Number of Products" "Cluster SE clusters:"   "NumClust Number of Clusters" ) 
		

	esttab using $results\T3_Table_3.tex, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("PR50_5 Price Ratio (Size 50 to 5)" "PR500_5 Price Ratio (Size 500 to 5)" "j \hline" "Sample Sample"  "Winsor Winsor" Observations "k \hline "   "Fixed1 State x Rural x Product FE" "Cluster SE clusters:"   "NumClust Number of Clusters" ) 
		
		eststo clear
		
		
			* Non-Parametric
		xi: areg ln_price_winsor1 ln_L_winsor i.state*i.rural, absorb(product_unit_cat)
		xi: areg ln_L_winsor i.state*i.rural, absorb(product_unit_cat) 
		predict ln_L_resid_st_rur, resid
		xi: areg ln_price_winsor1 i.state*i.rural, absorb(product_unit_cat) vce(cluster product_unit_cat_old)
		predict ln_price_resid_st_rur, resid
		reg  ln_price_resid_st_rur ln_L_resid_st_rur
		*lpoly ln_price_resid ln_L_resid, noscatter 
		su ln_L_resid_st_rur, de
		local ln_L_resid_lb = `r(p1)'
		local ln_L_resid_ub = `r(p99)'
		lpoly ln_price_resid_st_rur ln_L_resid_st_rur if ln_L_resid_st_rur>`ln_L_resid_lb'&ln_L_resid_st_rur<`ln_L_resid_ub', nograph gen(ln_L_poly ln_price_poly) se(lpoly_se)
		su ln_L_poly, de
		if floor(`r(min)') - `r(min)' <-0.5 {
			local ln_L_label_lb = ceil(`r(min)') - 0.5
		}
		else {
			local ln_L_label_lb = floor(`r(min)')
		}
		su ln_L_poly, de
		if ceil(`r(max)') - `r(max)' >0.5 {
			local ln_L_label_ub = floor(`r(max)') + 0.5
		}
		else {
			local ln_L_label_ub = ceil(`r(max)')
		}
		
		egen price_range_lb = min( ln_price_poly - 2* lpoly_se) if ln_price_poly~=.
		egen price_range_ub = max( ln_price_poly + 2* lpoly_se) if ln_price_poly~=.
		
		su  price_range_lb
		if floor(`r(min)'*10) - `r(min)'*10 <-0.5 {
			local ln_price_label_lb = (ceil(`r(min)')*10 - 0.5)/10
		}
		else {
			local ln_price_label_lb = floor(`r(min)'*10)/10
		}
		
		su  price_range_ub
		if ceil(`r(max)'*10) - `r(max)'*10 >0.5 {
			local ln_price_label_ub = (floor(`r(max)')*10 + 0.5)/10
		}
		else {
			local ln_price_label_ub = ceil(`r(max)'*10)/10
		}
		
		
// 				su ln_L_poly, de
// 		#delimit ;
// 		lpoly ln_price_resid_st_rur ln_L_resid_st_rur if ln_L_resid_st_rur>`ln_L_resid_lb'&ln_L_resid_st_rur<`ln_L_resid_ub', noscatter ci
// 		title("Price-Size Relation: ASI and SUM")
// 		xlabel(`ln_L_label_lb'(1)`ln_L_label_ub',labsize(3.5)) ylabel(`ln_price_label_lb'(0.2)`ln_price_label_ub',labsize(3.5))
// 		ytitle("log(Price) Residualized ", size(4.3)) xtitle("log(L) Residualized", size(4.3)) 
// 		note("")
// 		//xlabel(1(.5)4.2)
// 		graphregion(fcolor(white))
// 		legend(off)
// 		graphregion(lcolor(black))
// 		name(price_size_nonParam_st_rur, replace);
//		
// 		#delimit cr
// 		return list
// 		graph export "$graphs\price_size_st_rur_12split`split_prob_1_2'_4split`csplit_prob_4'.eps", name(price_size_nonParam_st_rur) as(eps) replace
		
// 		su ln_L_poly, de
// 		#delimit ;
// 		lpoly ln_price_resid_st_rur ln_L_resid_st_rur if ln_L_resid_st_rur>`ln_L_resid_lb'&ln_L_resid_st_rur<`ln_L_resid_ub', noscatter ci
// 		title("")
// 		xlabel(`ln_L_label_lb'(1)`ln_L_label_ub',labsize(3.5)) ylabel(`ln_price_label_lb'(0.2)`ln_price_label_ub',labsize(3.5))
// 		ytitle("log(Price) Residualized ", size(4.3)) xtitle("log(L) Residualized", size(4.3)) 
// 		note("")
// 		//xlabel(1(.5)4.2)
// 		graphregion(fcolor(white))
// 		legend(off)
// 		graphregion(lcolor(black))
// 		name(price_size_nonParam_st_rur, replace);
//		
// 		#delimit cr
// 		return list
// 		graph export "$graphs\price_size_st_rur_12split`split_prob_1_2'_4split`csplit_prob_4'_noT.eps", name(price_size_nonParam_st_rur) as(eps) replace
		
			su ln_L_poly, de
		#delimit ;
		lpoly ln_price_resid_st_rur ln_L_resid_st_rur if ln_L_resid_st_rur>`ln_L_resid_lb'&ln_L_resid_st_rur<`ln_L_resid_ub', noscatter ci
		kernel(epanechnikov) bwidth(0.502)
		title("")
// 		yscale(range(-0.3 0.5))
// 		ylabel(-0.6(0.2)0.6, labsize(3.5))
		xlabel(`ln_L_label_lb'(1)`ln_L_label_ub',labsize(3.5)) ylabel(-0.3(0.2)0.4,labsize(3.5))
// 		xscale(range(-3 3))
		 ytitle("log(Price) Residualized ", size(4.3)) xtitle("log(L) Residualized", size(4.3)) 
		note("")
		//xlabel(1(.5)4.2)
		graphregion(fcolor(white))
		legend(off)
		graphregion(lcolor(black))
		name(price_size_nonParam_st_rur, replace);
		
		#delimit cr
		return list
		graph export "$results\F5_noT.eps", name(price_size_nonParam_st_rur) as(eps) replace
		
